/**
 * 从服务器请求包裹详情
 * @param self
 * @param tiyData
 * @returns {Array}
 */
import tiy from "../../../../site/Site";
import {afterModifyStandsBaseInfo, queryBoothList} from "../../../stands/js/stand/edit_funs";

/**
 * 加载物流包裹
 * @param self
 * @param tiyData
 * @param orderId
 * @param isFirst
 */
export const getPackageInfo = (self, tiyData, orderId,isFirst) => {
  self.$Spin.show();
  self.$apiNewOrder.getPackageInfo({
    "orderId": orderId
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let result = response.result;
      self.packageList = [...result.packages];
      self.productList = [...result.products];
      self.packageBoxList = [...result.packageBoxList];
      self.packBoxArray=[...result.packageBoxList];
      self.radioLabel2="全部";
      self.productListFilter = JSON.parse(JSON.stringify(self.productList));
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 查询未质检的产品
 * @param self
 * @param tiyData
 * @param orderId
 */
export const queryNotQualityProducts = (self, tiyData, orderId) => {
  self.$Spin.show();
  self.$apiNewOrder.queryNotQualityProducts({
    "orderId": orderId
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let result = response.result;
      self.notQualityCheckProducts = [...result];
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};



/**
 * 新建物流包裹
 * @param self
 * @param tiyData
 * @param name
 */
export const addPackage = function (self, tiyData, name) {
  self.$refs[name].validate((valid) => {
    if (valid) {
      tiyData.tStatus.modalPackage.isLoading = true;
      self.$apiNewOrder.addPackage(self.package)
        .then(function (response) {
          changeModelLoading(self, tiyData);
          if (response.success) {
            tiyData.tStatus.modalPackage.isShow = false;
            self.$Message.success('创建成功!');
            getPackageInfo(self, tiyData, self.package.orderId,false);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      changeModelLoading(self, tiyData)
    }
  });
};

/**
 * 新建实体箱包
 * @param self
 * @param tiyData
 * @param name
 */
export const addPackageBox = function (self, tiyData, name) {
  self.$refs[name].validate((valid) => {
    if (valid) {
      tiyData.tStatus.modalPackageBox.isLoading = true;
      self.$apiNewOrder.addPackageBox(self.packageBox)
        .then(function (response) {
          changeModelLoading(self, tiyData);
          if (response.success) {
            tiyData.tStatus.modalPackageBox.isShow = false;
            self.$Message.success('创建成功!');
            getPackageInfo(self, tiyData, self.package.orderId,false);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      changeModelLoading(self, tiyData)
    }
  });
};

/**
 * 包裹添加产品
 * @param self
 * @param tiyData
 * @param name
 */
export const addProduct = function (self, tiyData, name) {
  if(tiyData.tParams.addProduct.colorItemIds.length <1){
    self.$Message.error("请选择产品");
    changeModelLoading(self, tiyData);
    return;
  }
  self.$refs[name].validate((valid) => {
    if (valid) {
      let params={
        "boxProducts":tiyData.tParams.addProduct.colorItemIds,
      };
      tiyData.tStatus.modalProductAdd.isLoading = true;
      self.$apiNewOrder.packageBoxAddProduct(params)
        .then(function (response) {
          changeModelLoading(self, tiyData);
          if (response.success) {
            tiyData.tStatus.modalProductAdd.isShow = false;
            tiyData.tStatus.modalBatchProductAdd.isShow = false;
            self.$Message.success('添加成功!');
            getPackageInfo(self, tiyData, self.package.orderId,false);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      changeModelLoading(self, tiyData);
    }
  });
};

//删除包裹
export const deletepackageBox = (self,tiyData, box) => {
  self.$Modal.confirm({
    title: "确认",
    content: "确定要删除"+box.name+"吗？此操作无法撤回",
    okText: "删除",
    cancelText: "取消",
    onOk() {
      let params = {"id": box.id};
      self.$Spin.show();
      self.$apiNewOrder.deletePackageBox(params).then(response => {
        if(response.success) {
          self.$Message.success("已经删除！");
          getPackageInfo(self, tiyData, self.package.orderId,false);
        }else{
          self.$Message.success(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

//修改包裹下产品数量
export const updatePackageBoxProduct = function (self, tiyData, name) {
  self.$refs[name].validate((valid) => {
    if (valid) {
      tiyData.tStatus.modalBoxCountModify.isLoading = true;
      let params={
        "id":tiyData.tData.modifyBoxCount.id,
        "boxCount":tiyData.tData.modifyBoxCount.boxCount,
      }
      self.$apiNewOrder.updatePackageBoxProduct(params)
        .then(function (response) {
          changeModelLoading(self, tiyData);
          if (response.success) {
            tiyData.tStatus.modalBoxCountModify.isShow = false;
            self.$Message.success('修改成功!');
            getPackageInfo(self, tiyData, self.package.orderId,false);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      changeModelLoading(self, tiyData)
    }
  });
};


function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalPackage.isLoading = false;
  tiyData.tStatus.modalPackageBox.isLoading = false;
  tiyData.tStatus.modalProductAdd.isLoading = false;
  tiyData.tStatus.modalBatchProductAdd.isLoading = false;
  tiyData.tStatus.modalBoxCountModify.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalPackage.isLoading = true;
    tiyData.tStatus.modalPackageBox.isLoading = true;
    tiyData.tStatus.modalProductAdd.isLoading = true;
    tiyData.tStatus.modalBatchProductAdd.isLoading = true;
    tiyData.tStatus.modalBoxCountModify.isLoading = true;
  });
}

